package com.ming.sps.server.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.*;

public class SimilarityUtils {

    //阈值
    public static double YUZHI = 0.001 ;

    /**
     * 返回百分比
     */
    public static double getSimilarity(List<String> T1, List<String> T2) throws Exception {
        int size = 0 , size2 = 0 ;
        if ( T1 != null && ( size = T1.size() ) > 0 && T2 != null && ( size2 = T2.size() ) > 0 ) {

            Map<String, double[]> T = new HashMap<String, double[]>();

            //T1和T2的并集T
            String index = null ;
            for ( int i = 0 ; i < size ; i++ ) {
                index = T1.get(i) ;
                if( index != null){
                    double[] c = T.get(index);
                    c = new double[2];
                    c[0] = 1;	//T1的语义分数Ci
                    c[1] = YUZHI;//T2的语义分数Ci
                    T.put( index, c );
                }
            }

            for ( int i = 0; i < size2 ; i++ ) {
                index = T2.get(i) ;
                if( index != null ){
                    double[] c = T.get( index );
                    if( c != null && c.length == 2 ){
                        c[1] = 1; //T2中也存在，T2的语义分数=1
                    }else {
                        c = new double[2];
                        c[0] = YUZHI; //T1的语义分数Ci
                        c[1] = 1; //T2的语义分数Ci
                        T.put( index , c );
                    }
                }
            }

            //开始计算，百分比
            Iterator<String> it = T.keySet().iterator();
            double s1 = 0 , s2 = 0, Ssum = 0;  //S1、S2
            while( it.hasNext() ){
                double[] c = T.get( it.next() );
                Ssum += c[0]*c[1];
                s1 += c[0]*c[0];
                s2 += c[1]*c[1];
            }
            //百分比
            return Ssum / Math.sqrt( s1*s2 );
        } else {
            throw new Exception("相似度计算工具类传入参数有问题！");
        }
    }

    public static byte[] bt;
    public static InputStream is;
    public static Reader read;
    public static Lexeme t;
    public static IKSegmenter iks;

    /**
     * input:str
     * 将字符串分词转换成数组
     */
    public static List<String> stringToArray(String str) {

        List<String> list = new ArrayList<String>();
        bt = str.getBytes();
        is = new ByteArrayInputStream(bt);
        read = new InputStreamReader(is);
        iks = new IKSegmenter(read, true);
        try {
            while ((t = iks.next()) != null) {
                if(t.getLength() <= 1){
                    continue;
                }
                list.add(t.getLexemeText());
            }
        } catch (Exception e) {

        }
        return list;
    }

    public static void main(String[] args) throws Exception {
        List<String> v1 = stringToArray("广东省广州市天河区车陂高地大街18号");
        List<String> v2 = stringToArray("广东省广州市天河区车陂片区");
        System.out.println(getSimilarity(v1, v2));
    }
}
